package wang.jinggo.util;

import java.io.Serializable;

/**
 * @author tanljs
 * @date 2017-07-13
 */
public class PageTurn implements Serializable {
    private Long firstPage = 0l;//首页
    private Long pageCount = 0l;//总页数
    private Long pageNo = 0l;//当前页
    private Long nextPage = 0l;//下一页
    private Long prevPage = 0l;//上一页
    private Long pageSize = 0l;//每页记录数
    private Long rowCount = 0l;//总记录数

    public PageTurn(Long pageNo, Long pageSize, Long rowCount) {
        initPage((pageNo - 1) * pageSize, pageSize, rowCount);
    }

    private void initPage(Long startRow, Long pageSize, Long rowCount) {
        if (rowCount <= 0 || pageSize <= 0) {
            return;
        }
        this.firstPage = 1l;
        this.rowCount = rowCount;
        this.pageSize = pageSize;
        this.pageCount = (long) Math.ceil((double) rowCount / pageSize);

        Long startResult = startRow < 0 ? 0 : startRow;
        if (startResult > rowCount) {
            startResult = rowCount - pageSize;
        }
        if (startResult < 0) {
            startResult = 0l;
        }
        Long pageResult = startResult / pageSize + 1;
        if (pageResult > pageCount.longValue()) {
            pageResult = pageCount.longValue();
        }
        this.pageNo = pageResult;

        Long nextPageResult = pageResult + 1;
        nextPageResult = nextPageResult >= pageCount.intValue() ? pageCount.intValue() : nextPageResult;
        this.nextPage = nextPageResult;

        this.prevPage = pageResult > 1 ? (pageResult - 1) : 1;
    }

    public Long getFirstPage() {
        return firstPage;
    }

    public void setFirstPage(Long firstPage) {
        this.firstPage = firstPage;
    }

    public Long getPageCount() {
        return pageCount;
    }

    public void setPageCount(Long pageCount) {
        this.pageCount = pageCount;
    }

    public Long getPageNo() {
        return pageNo;
    }

    public void setPageNo(Long pageNo) {
        this.pageNo = pageNo;
    }

    public Long getNextPage() {
        return nextPage;
    }

    public void setNextPage(Long nextPage) {
        this.nextPage = nextPage;
    }

    public Long getPrevPage() {
        return prevPage;
    }

    public void setPrevPage(Long prevPage) {
        this.prevPage = prevPage;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public Long getRowCount() {
        return rowCount;
    }

    public void setRowCount(Long rowCount) {
        this.rowCount = rowCount;
    }

    @Override
    public String toString() {
        return "PageTurn{" +
                "firstPage=" + firstPage +
                ", pageCount=" + pageCount +
                ", pageNo=" + pageNo +
                ", nextPage=" + nextPage +
                ", prevPage=" + prevPage +
                ", pageSize=" + pageSize +
                ", rowCount=" + rowCount +
                '}';
    }
}
